package cn.nbt.busManage.service.impl;

import cn.nbt.busManage.dao.mapper.BusMapper;
import cn.nbt.busManage.dao.mapper.DriverMapper;
import cn.nbt.busManage.dao.mapper.ScheduleMapper;
import cn.nbt.busManage.model.dto.DriverDto;
import cn.nbt.busManage.model.dto.ScheduleDto;
import cn.nbt.busManage.model.entity.Driver;
import cn.nbt.busManage.model.entity.Schedule;
import cn.nbt.busManage.dao.repository.DriverRepository;
import cn.nbt.busManage.service.DriverService;
import jakarta.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


@Service
@Transactional
public class IDriverService implements DriverService {

    @Autowired
    private DriverMapper driverMapper;

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Autowired
    private DriverRepository driverRepository;

    @Autowired
    private EntityManager entityManager;

    @Autowired
    private BusMapper busMapper;

    /**
     * 查询所有司机
     * @return 司机信息
     */
    @Override
    public List<DriverDto> findAllDriver(){
        Iterable<Driver> all = driverRepository.findAll();
        ArrayList<DriverDto> driverDtos = new ArrayList<>();
        for(Driver driver : all){
            driverDtos.add(new DriverDto(driver));
        }
        return driverDtos;
    }

    /**
     * 添加司机
     * @param driverDto 司机信息
     */
    public void addDriver(DriverDto driverDto){
        Driver driver = new Driver();
        driver.setDriverName(driverDto.getDriverName());
        driver.setBirthday(driverDto.getBirthday());
        driver.setAddress(driverDto.getAddress());
        driver.setCell(driverDto.getCell());
        if(driverDto.getSchedules()!=null){
            ArrayList<Schedule> schedules = new ArrayList<>();
            for (ScheduleDto scheduleDto : driverDto.getSchedules()) {
                Schedule schedule = entityManager.find(Schedule.class, scheduleDto.getId());
                schedules.add(schedule);
            }
            driver.setSchedules(schedules);
        }
        entityManager.persist(driver);
    }

    /**
     * 更新司机
     * @param driverDto 司机信息
     */
    public void updateDriver(DriverDto driverDto){
        Driver driver = new Driver();
        driver.setId(driverDto.getId());
        driver.setDriverName(driverDto.getDriverName());
        driver.setBirthday(driverDto.getBirthday());
        driver.setAddress(driverDto.getAddress());
        driver.setCell(driverDto.getCell());
        if(driverDto.getSchedules()!=null){
            ArrayList<Schedule> schedules = new ArrayList<>();
            for (ScheduleDto scheduleDto : driverDto.getSchedules()) {
                Schedule schedule = entityManager.find(Schedule.class, scheduleDto.getId());
                schedules.add(schedule);
            }
            driver.setSchedules(schedules);
        }
        entityManager.merge(driver);
    }
    /**
     * 删除司机
     * @param driverId 司机id
     */

    @Transactional("mybatisTransactionManager")
    public void deleteDriver(Integer driverId){
        if (driverMapper.findByDriverId(driverId) == null) {
            throw new RuntimeException("司机不存在");
        }
        busMapper.deleteBusDriver(driverId);
        scheduleMapper.deleteDriverSchedule(driverId);
        driverMapper.deleteDriver(driverId);
    }

    /**
     * 根据司机名称查询
     * @param driverName 司机名称
     * @return 司机信息
     */
    public List<DriverDto> findByName(String driverName){
        List<Driver> drivers = driverRepository.findByDriverName(driverName);
        if (drivers.isEmpty()) {
            throw new RuntimeException("司机不存在");
        }
        ArrayList<DriverDto> driverDtos = new ArrayList<>();
        for (Driver driver : drivers) {
            driverDtos.add(new DriverDto(driver));
        }
        return driverDtos;
    }
    /**
     * 根据司机id查询
     * @param driverId 司机id
     * @return 司机信息
     */
    public DriverDto findByDriverId(Integer driverId){
        Optional<Driver> byId = driverRepository.findById(driverId);
        return byId.map(DriverDto::new).orElse(null);
    }

    /**
     * 根据司机名称查询
     * @param driverName 司机名称
     * @return 司机信息
     */
    @Override
    @Transactional("mybatisTransactionManager")
    public DriverDto findByDName(String driverName) {
        Driver driver = driverMapper.findByDName(driverName);
        if (driver != null) {
            List<Schedule> scheduleDtos = scheduleMapper.findByDriverId(driver.getId());
            driver.setSchedules(scheduleDtos);
            return new DriverDto(driver);
        }
        return null;
    }

}
